<template>
  <div class="cityList_wrapper" ref="cityList">
    <div>
      <div class="area" v-show="isShowCurrentCity">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="button-wrapper" v-for="item of hotcity" :key="item.id" @click="handleCityClick(item.name, item.id)">
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) of groupCitySort" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom" v-for="innerItem of item" :key="innerItem.id" @click="handleCityClick(innerItem.name, item.id)">{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import BScroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotcity: Array,
    groupcity: Object,
    letter: String
  },
  computed: {
    ...mapState(['city', 'cityId']),

    isShowCurrentCity () {
      return this.city === '选择城市' ? 0 : 1
    },

    // 将获取的groupcity数据按照A-Z字母开头排序
    groupCitySort () {
      let sortobj = {}
      for (let i = 65; i <= 90; i++) {
        if (this.groupcity[String.fromCharCode(i)]) {
          sortobj[String.fromCharCode(i)] = this.groupcity[String.fromCharCode(i)]
        }
      }
      return sortobj
    }
  },
  methods: {
    handleCityClick (city, cityId) {
      this.CHANGE_CITY({ city, cityId })
      // this.$emit('close')
      this.$listeners.close()
    },
    ...mapMutations(['CHANGE_CITY'])
  },
  watch: {
    letter () {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element, 1000)
      }
    }
  },
  mounted () {
    this.scroll = new BScroll(this.$refs.cityList, {
      mouseWheel: {
        speed: 20,
        invert: false,
        easeTime: 300
      }
    })
  }
}
</script>

<style lang="stylus" scoped>
  .border-topbottom
    &:after
      border-color: #ddd
  .border-bottom
    &:before
      border-color: #ddd
  .cityList_wrapper
    overflow: hidden
    position: absolute
    top: 1.78rem
    left: 0
    right: 0
    bottom: 1rem
    .area
      font-size: .28rem
      .title
        line-height: .31rem
        background: #f5f5f5
        padding-left: .2rem
        color: #666
        padding: .3rem 0 .14rem .3rem
      .button-list
        overflow: hidden
      .button-wrapper
          width: 25%
          float: left
          .button
            line-height: 1rem
            padding-left: .3rem
      .item-list
        padding-left: .3rem
        .item
          line-height: 1rem
</style>
